package bubble;

import java.awt.Color;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JTextPane;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JLabel;
import javax.swing.JSpinner;
import javax.swing.JProgressBar;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;
import javax.swing.event.ChangeListener;
import javax.swing.event.ChangeEvent;
import javax.swing.JPanel;

public class APIW {

	private JFrame frame;
	static int NUMBER;
	static private JSpinner spinner;
	static public JTextPane[] textPane2;
	static private JLabel label;
	static private JButton btnNewButton_1;
	static private JComboBox comboBox;
	static private JLabel label_1;
	static public int[] array;
	public static JTextPane[] textPane1;
	private JPanel panel_1;
	static private JPanel panel;

	

	public APIW() {
		initialize();
	}

	private void initialize() {
		setFrame(new JFrame());
		getFrame().setResizable(false);
		getFrame().setBounds(100, 100, 576, 355);
		getFrame().setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		JButton btnNewButton = new JButton("Start");
		btnNewButton.setBounds(69, 231, 369, 40);
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Runnable r = new NewThread();
				Thread t = new Thread(r);
				t.start();
			}
		});
		getFrame().getContentPane().setLayout(null);
		getFrame().getContentPane().add(btnNewButton);
		label = new JLabel(
				"\u041A\u043E\u043B\u0438\u0447\u0435\u0441\u0442\u0432\u043E \u044D\u043B\u0435\u043C\u0435\u043D\u0442\u043E\u0432:");
		label.setBounds(10, 11, 139, 17);
		getFrame().getContentPane().add(label);
		btnNewButton_1 = new JButton("Ok");
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				array = new int[NUMBER];
				try {
					switch (comboBox.getSelectedIndex()) {
					case 0:
						bubble.Generation.randomGen(array, 1, 40);
						break;

					case 1:
						bubble.Generation.orderedGen(array);
						break;

					case 2:
						bubble.Generation.reverseGen(array);
						break;
					default:
						bubble.Generation.randomGen(array, 1, 40);
						break;
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
				PrintPanes(array);
			}
		});
		btnNewButton_1.setBounds(383, 53, 100, 24);
		getFrame().getContentPane().add(btnNewButton_1);

		spinner = new JSpinner();
		spinner.addChangeListener(new ChangeListener() {
			public void stateChanged(ChangeEvent arg0) {
				NUMBER = (int) spinner.getValue();
				if ( NUMBER > 20 ) NUMBER = 20; 
			}
		});
		spinner.setBounds(159, 9, 49, 20);
		getFrame().getContentPane().add(spinner);

		label_1 = new JLabel(
				"\u0421\u043F\u043E\u0441\u043E\u0431 \u0437\u0430\u043F\u043E\u043B\u043D\u0435\u043D\u0438\u044F");
		label_1.setBounds(10, 55, 139, 22);
		getFrame().getContentPane().add(label_1);

		comboBox = new JComboBox();
		comboBox.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				array = new int[NUMBER];
				try {
					switch (comboBox.getSelectedIndex()) {
					case 0:
						bubble.Generation.randomGen(array, 1, 40);
						break;

					case 1:
						bubble.Generation.orderedGen(array);
						break;

					case 2:
						bubble.Generation.reverseGen(array);
						break;

					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
		comboBox.setModel(new DefaultComboBoxModel(
				new String[] {
						"\u0421\u043B\u0443\u0447\u0430\u0439\u043D\u043E\u0435 \u0437\u0430\u043F\u043E\u043B\u043D\u0435\u043D\u0438\u0435",
						"\u041F\u0440\u044F\u043C\u043E\u0439 \u043F\u043E\u0440\u044F\u0434\u043E\u043A",
						"\u0412 \u043E\u0431\u0440\u0430\u0442\u043D\u043E\u043C \u043F\u043E\u0440\u044F\u0434\u043A\u0435" }));
		comboBox.setBounds(159, 54, 203, 24);
		getFrame().getContentPane().add(comboBox);

	
	}

	public void PrintPanes(int arr[]) {
		JPanel panel = new JPanel();
		getFrame().getContentPane().add(panel);
		panel.setBounds(10, 135, 640, 23);
		panel.setLayout(null);
		textPane2 = new JTextPane[NUMBER];
		Color c = new Color(30,144,255);
		for (int i = 0; i < arr.length; i++) {
			
			textPane2[i] = new JTextPane();
			textPane2[i].setEditable(false);
			textPane2[i].setForeground(Color.BLACK);
			String str2 = arr[i] + " ";
			textPane2[i].setText(str2);
			textPane2[i].setBackground(c);
			textPane2[i].setBounds(i * 28, 0, 22, 40);
			panel.add(textPane2[i]);
		}
	}

	public JFrame getFrame() {
		return frame;
	}

	public void setFrame(JFrame frame) {
		this.frame = frame;
	}

}